package com.ydd.service.impl;

import com.ydd.dto.MenuDTO;
import com.ydd.entity.SysMenu;
import com.ydd.exception.BaseException;
import com.ydd.mapper.SysMenuMapper;
import com.ydd.mapper.SysRoleMenuMapper;
import com.ydd.service.SysMenuService;
import com.ydd.vo.SysmenuVO;
import com.ydd.vo.common.MenuIdAndPid;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author fang
 * @Date 2023-05-29 16:15
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 菜单修改
     *
     * @param sysMenu
     */
    @Override
    public void updateMenu(MenuDTO sysMenu) {
        SysMenu sysMenu1 = new SysMenu();
        String regex=".+[@].+";
        boolean matches = sysMenu.getMark().matches(regex);
        if (matches==false){
            throw new BaseException("权限标识格式错误");
        }
        regex="/.+";
        boolean matches1 = sysMenu.getPath().matches(regex);
        if (matches1==false){
            throw new BaseException("路由路径格式错误");
        }
        BeanUtils.copyProperties(sysMenu, sysMenu1);
        sysMenu1.setIsCache(sysMenu.getIs_cache());
        sysMenu1.setIsLink(sysMenu.getIs_link());
        sysMenu1.setIsShow(sysMenu.getIs_show());
        sysMenu1.setUpdateDate(sysMenu.getUpdate_date());
        sysMenuMapper.updateById(sysMenu1);
        //sysMenu.setUpdateDate(LocalDateTime.now());
        //sysMenuMapper.updateById(sysMenu);
    }

    /**
     * 菜单删除
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteMenu(String id) {
        Integer ID = Integer.parseInt(id);
        List<SysmenuVO> recursion = recursion(ID.longValue());
        if (recursion!=null&&recursion.size()>0){
            throw new BaseException("该菜单包含子菜单，不能删除");
        }

        sysRoleMenuMapper.deleteByMenuId(ID);
        sysMenuMapper.deleteMenuById(ID);

    }

    /**
     * 查询所有菜单
     *
     * @return
     */
    @Override
    public List<SysmenuVO> allData(Long pId) {
        //先查询父级为1的菜单
        List<SysmenuVO> sysmenuVOS = sysMenuMapper.getByPid(pId);
        if (sysmenuVOS != null) {//查询到1级菜单后，根据1级菜单的id查询查询子菜单
            for (SysmenuVO sysmenuall : sysmenuVOS) {
                List<SysmenuVO> sysmenuVOList = recursion(sysmenuall.getId());
                //查询到子菜单赋给Children
                sysmenuall.setChildren(sysmenuVOList);
            }
        }
        return sysmenuVOS;
    }

    /**
     * 递归查询子菜单
     *
     * @param pid
     * @return
     */
    private List<SysmenuVO> recursion(Long pid) {
        //根据父级id查询到当级菜单
        List<SysmenuVO> sysMenuVOs = sysMenuMapper.getByPid(pid);
        if (sysMenuVOs != null) {//当级菜单不为null
            //遍历集合
            for (SysmenuVO sysMenu : sysMenuVOs) {
                //将当级id当作下级的父级id传下去
                List<SysmenuVO> sysMenuList = recursion(sysMenu.getId());
                sysMenu.setChildren(sysMenuList);
            }
        }
        return sysMenuVOs;
    }

    /**
     * 菜单添加
     *
     * @param sysMenu
     */
    @Override
    public void addMenu(MenuDTO sysMenu) {
        SysMenu sysMenu1 = new SysMenu();
        String regex=".+[@].+";
        boolean matches = sysMenu.getMark().matches(regex);
        if (matches==false){
            throw new BaseException("权限标识格式错误");
        }
        regex="/.+";
        boolean matches1 = sysMenu.getPath().matches(regex);
        if (matches1==false){
            throw new BaseException("路由路径格式错误");
        }
        BeanUtils.copyProperties(sysMenu, sysMenu1);
        sysMenu1.setIsCache(sysMenu.getIs_cache());
        sysMenu1.setIsLink(sysMenu.getIs_link());
        sysMenu1.setIsShow(sysMenu.getIs_show());
        sysMenu1.setUpdateDate(LocalDateTime.now());
        sysMenu1.setCreateDate(LocalDateTime.now());
        sysMenuMapper.insert(sysMenu1);
    }

    /**
     * 根据id查询菜单详情
     *
     * @param id
     * @return
     */
    @Override
    public SysmenuVO getMenuById(Integer id) {
        SysmenuVO sysMenu = sysMenuMapper.getMenuById(id);
        return sysMenu;
    }

    /**
     * 根据Pid和id查询连菜单
     *
     * @param mad
     * @return
     */
    @Override
    public SysmenuVO getMenuByIdAndPid(MenuIdAndPid mad, List<String> flag) {
        //先根据id查询菜单链的头菜单
        SysmenuVO sysmenuVO = sysMenuMapper.getMenuByIdVO(mad.getId());
        if (flag != null && flag.contains(mad.getId())) {//判断该菜单是已获取
            flag.remove(sysmenuVO.getId().toString());//该菜单已获取，移除
            if (sysmenuVO != null) {
                //头菜单不为null
                //查询到子菜单赋给Children
                sysmenuVO.setChildren(recursionRowInfo(sysmenuVO.getId(), flag));
            }
            return sysmenuVO;
        } else {
            return null;
        }
    }

    /**
     * 角色详情-递归查询菜单
     *
     * @param pid
     * @return
     */
    private List<SysmenuVO> recursionRowInfo(Long pid, List<String> flag) {
        //根据父级id查询到pid下的菜单集合
        List<SysmenuVO> sysMenuVOs = sysMenuMapper.getByPid(pid);
        List<SysmenuVO> newSysMenu = new ArrayList<>();
        if (sysMenuVOs != null) {//菜单集合不为null
            //遍历集合
            for (SysmenuVO sysMenu : sysMenuVOs) {
                if (sysMenu != null && flag != null && flag.contains(sysMenu.getId().toString())) {//菜单不为null，并且没有获取
                    flag.remove(sysMenu.getId().toString());//移除
                    //将当级id当作下级的父级id传下去
                    List<SysmenuVO> sysMenuList = recursionRowInfo(sysMenu.getId(), flag);
                    sysMenu.setChildren(sysMenuList);
                    newSysMenu.add(sysMenu);
                }
            }
        }
        return newSysMenu;
    }

}
